<?php

class InitializerPlugin extends Zend_Controller_Plugin_Abstract
{
	var $enviroment;
	
    public function __construct($env)
    {
        $this->setEnv($env);
    }

    public function routeStartup(Zend_Controller_Request_Abstract $request)
    {
        $this->initRegistry()
             ->loadConfig()
             ->initView()   
             ->initDb()
             ->setPlugins()
             ->setControllerDirectory();
    }

    public function setEnv($env)
    {
    	//
    	$this->enviroment = $env;
    	
    	// Some things that everyone seem to do
    	error_reporting(E_ALL|E_STRICT);
        ini_set('display_startup_errors', true);
		ini_set('display_errors', true);
        date_default_timezone_set('Europe/Copenhagen');
    }
    
    public function initRegistry()
    {
    	// This allows to access the registry as a object (eg. $registry->config etc.)
    	// http://framework.zend.com/manual/en/zend.registry.html#zend.registry.using.array-object
    	$registry = new Zend_Registry(array(), ArrayObject::ARRAY_AS_PROPS);
		Zend_Registry::setInstance($registry);
		
		return $this;
    }
    
    public function loadConfig()
    {
    	// Load the config according to the enviroment and assign it to the registry
    	$config = new Zend_Config_Ini('../application/config.ini', $this->enviroment);
    	Zend_Registry::getInstance()->configuration = $config;
    	
    	return $this;
    }
    
    public function initView()
    {
    	$view = new Zend_View;
        $view->setEncoding('UTF-8');
        $viewRenderer = new Zend_Controller_Action_Helper_ViewRenderer($view);
        Zend_Controller_Action_HelperBroker::addHelper($viewRenderer);
        Zend_Layout::startMvc(
            array(
                'layoutPath' => '.../application/layouts',
                'layout' => 'layout'
            )
        );
        
        return $this;
    }
    
    public function initDb()
    {
    	// Connect to the database using the configuaration details
    	$config = Zend_Registry::getInstance()->configuration;
    	$db = Zend_Db::factory($config->db);
        $db->query("SET NAMES 'utf8'");
        
        // Guess this means that models using the Zend_Db_Table knows the db connection
        Zend_Db_Table::setDefaultAdapter($db);
        
        // Store the database connection in the registry
        Zend_Registry::getInstance()->database = $db;
	     
        return $this;
    }
    
    
    public function setPlugins() 
    {
    	$front = Zend_Controller_Front::getInstance();
		$front->registerPlugin(new SidebarPlugin());
		
		return $this;
    }
    
    
    public function setControllerDirectory()
    {
    	
    	$front = Zend_Controller_Front::getInstance();
    	$front->throwExceptions(true);
    	$front->returnResponse(true);
    	$front->setControllerDirectory( '../application/controllers' );
    	
    	return $this;
    }
    
}
